#标准库，用于创建图形用户界面（GUI），支持窗口、按钮、文本框等控件的创建和管理。
import tkinter as tk
from tkinter import PhotoImage
#第三方库，用于图像处理，支持图像打开、编辑、保存、格式转换等操作。Pillow 是 PIL 的现代分支，功能更强大且兼容性更好。
from PIL import Image, ImageTk, ImageDraw, ImageFont
from .Shared import *

player = shareddata.get_player()

#设置界面
class PageSet(tk.Frame):

    def __init__(self, parent):
        super().__init__(parent)
        self.configure(bg = "white", width = 1200, height = 720)
        # 禁止自动调整大小
        self.pack_propagate(False) 
        self.bind("<Expose>", lambda event: self.on_raise(event, parent))
        global player
        player = shareddata.get_player()

        #加载界面变量
        #主页界面
        self.pagebuttonlist = ["", "", "", "", ""]
        self.pagebuttontextlist = ["PageHome", "PageRole", "PageForge", "PageFight", "PageSet"]
        self.pagebuttonid = []
        self.nowpage = 4
        #加载图像资源
        self.picture = {
            "PageSet": shareddata.get_picture("PageSet"),
            "Resources": shareddata.get_picture("Resources")
        }
        #加载动画缓冲画布
        self.buffer_image = Image.new("RGBA", (1200, 720), (255, 255, 255, 0))
        self.canvas = tk.Canvas(self, width = 1200, height = 720, highlightthickness = 0, bd = 0)
        self.canvas.place(x = 0, y = 0)

        #界面按钮
        for i in range(5):
            self.pagebuttonlist[i] = tk.Canvas(self, bg = "#FFFFFF" if (i != self.nowpage) else "#87CEFA", width = 80, height = 80, highlightthickness = 0, bd = 0)
            self.pagebuttonlist[i].place(x = 600 + 120 * i, y = 10)
            self.pagebuttonid.append(self.pagebuttonlist[i].create_image(0, 0, anchor = tk.NW, image = self.picture["Resources"]["ButtonList"][i][1]))
            self.pagebuttonlist[i].bind("<Enter>", lambda event, i = i: self.mouse_enter(event, self.nowpage, i, self.pagebuttonlist[i]))
            self.pagebuttonlist[i].bind("<Leave>", lambda event, i = i: self.mouse_leave(event, self.nowpage, i, self.pagebuttonlist[i]))
            self.pagebuttonlist[i].bind("<Button-1>", lambda event, i = i: parent.show_frame(self.pagebuttontextlist[i]))

        #动画缓冲
        self.update_buffer(parent)

    #界面抬升触发事件
    def on_raise(self, event, parent):
        global player
        player = shareddata.get_player()
        self.update_buffer(parent) 

    #动画缓冲之画面上传
    def update_buffer(self, parent):
        self.buffer_image.paste((255, 255, 255, 0), [0, 0, 1200, 720])
        self.draw_to_buffer(self.buffer_image, parent)
        self.photo_buffer = ImageTk.PhotoImage(self.buffer_image)
        self.canvas.create_image(0, 0, image = self.photo_buffer, anchor = tk.NW)

    #动画缓冲之绘制缓冲画面
    def draw_to_buffer(self, buffer_image, parent):
        global player
        player = shareddata.get_player()
        draw = ImageDraw.Draw(buffer_image)
        headfont = shareddata.get_font(30)
        draw.rectangle([0, 0, 1200, 720], fill=(255, 255, 255, 0))  
        #背景
        buffer_image.paste(self.picture["PageSet"]["BackGround"][0], (0, 0), self.picture["PageSet"]["BackGround"][0]) 
        #左上头像框   
        buffer_image.paste(self.picture["Resources"]["HeadFrame"][0], (0, 0), self.picture["Resources"]["HeadFrame"][0]) 
        buffer_image.paste(self.picture["Resources"]["KianaHead"][0], (20, 10), self.picture["Resources"]["KianaHead"][0]) 
        buffer_image.paste(self.picture["Resources"]["Crystal"][0], (120, 50), self.picture["Resources"]["Crystal"][0]) 
        buffer_image.paste(self.picture["Resources"]["Coin"][0], (270, 50), self.picture["Resources"]["Coin"][0]) 
        draw.text((120, 15), player["neckname"], font = headfont) 
        draw.text((270, 15), "LV." + str(player["level"]), font = headfont) 
        draw.text((150, 50), ":" + str(player["crystal"]), font = headfont) 
        draw.text((300, 50), ":" + str(player["coin"]), font = headfont) 
    
    def mouse_enter(self, event, cls, now, canvas):
        canvas.config(bg = "#FFD700" if (now != cls) else "#87CEFA")

    def mouse_leave(self, event, cls, now, canvas):
        canvas.config(bg = "#FFFFFF"  if (now != cls) else "#87CEFA")